/*
 * PreferenceUtils.java
 * business
 *
 * Created by ChenTao on 2016/9/9.
 *
 * Copyright (c) 2016年 yidiandao. All rights reserved.
 */
package com.yidiandao.business.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.text.TextUtils;

import com.yidiandao.business.constant.Preferences;
import com.yidiandao.business.data.bean.Tag;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

/**
 * SharedPreferences工具.
 */
public final class PreferenceUtils {

    /**
     * 整型变量默认值.
     */
    public static final int INT_DEFAULT = -9999999;

    /**
     * 长整形变量默认值.
     */
    public static final long LONG_DEFAULT = -9999999L;

    /**
     * 字符串变量默认值.
     */
    public static final String STRING_DEFAULT = "string_default";

    /**
     * 浮点型变量默认值.
     */
    public static final float FLOAT_DEFAULT = -0.999999f;

    /**
     * 布尔型变量默认值.
     */
    public static final boolean BOOLEAN_DEFAULT = false;

    private PreferenceUtils() {
    }

    /**
     * 设置登录用户的帐号.
     *
     * @param context Context
     * @param account 帐号
     */
    public static void setAccount(final Context context, final String account) {
        setString(context, Preferences.ACCOUNT, account, true);
    }

    /**
     * 获取登录用户的帐号.
     *
     * @param context Context
     * @return 登录用户的帐号, 若没有则返回默认值{@link #STRING_DEFAULT}
     */
    public static String getAccount(final Context context) {
        return getString(context, Preferences.ACCOUNT, true);
    }

    /**
     * 设置登录用户的Token.
     *
     * @param context Context
     * @param token   Token
     */
    public static void setToken(final Context context, final String token) {
        setString(context, Preferences.TOKEN, token, true);
    }

    /**
     * 获取登录用户的Token.
     *
     * @param context Context
     * @return 登录用户的Token, 若没有则返回默认值{@link #STRING_DEFAULT}
     */
    public static String getToken(final Context context) {
        return getString(context, Preferences.TOKEN, true);
    }

    /**
     * 获取登录用户的用户标识.
     *
     * @param context Context
     * @return 登录用户的标识, 若没有则返回默认值{@link #STRING_DEFAULT}
     */
    public static String getUserId(final Context context) {
        return getString(context, Preferences.UID, true);
    }

    /**
     * 设置登录用户的标识.
     *
     * @param context Context
     * @param userId  用户标识
     */
    public static void setUserId(final Context context, final String userId) {
        setString(context, Preferences.UID, userId, true);
    }

    /**
     * 设置用户昵称.
     *
     * @param context  Context
     * @param nickname 用户昵称
     */
    public static void setNickName(final Context context, final String nickname) {
        setString(context, Preferences.NICK_NAME, nickname, false);
    }

    /**
     * 获取用户昵称.
     *
     * @param context Context
     * @return 用户昵称
     */
    public static String getNickName(final Context context) {
        return getString(context, Preferences.NICK_NAME, false);
    }

    /**
     * 设置用户头像.
     *
     * @param context Context
     * @param sex     用户性别
     */
    public static void setSex(final Context context, final String sex) {
        setString(context, Preferences.SEX, sex, false);
    }

    /**
     * 获取用户头像.
     *
     * @param context Context
     * @return 用户性别
     */
    public static String getSex(final Context context) {
        return getString(context, Preferences.SEX, false);
    }

    /**
     * 清除登录信息.
     *
     * @param context Context
     */
    public static void clearLoginInfo(final Context context) {
        SharedPreferences sharedPreferences = getCommonPreferences(context);
        Editor editor = sharedPreferences.edit();
        editor.remove(Preferences.ACCOUNT);
        editor.remove(Preferences.TOKEN);
        editor.remove(Preferences.UID);
        editor.commit();
    }

    /**
     * 清除用户的所有信息.
     *
     * @param context Context
     */
    public static void clearAllInfo(final Context context) {
        SharedPreferences sharedPreferences = getAccountPreferences(context);
        if (sharedPreferences != null) {
            Editor editor = sharedPreferences.edit();
            Map<String, ?> map = sharedPreferences.getAll();
            String key;
            for (Entry<String, ?> entry : map.entrySet()) {
                key = entry.getKey();
                editor.remove(key);
            }
            editor.commit();
        }
    }

    /**
     * 保存字符串型值.
     *
     * @param context  Context
     * @param key      键
     * @param value    值
     * @param isCommon 是否属于公共属性, true - 属于公共属性, false - 属于私有属性
     */
    public static void setString(
            final Context context, final String key, final String value, final boolean isCommon) {
        if (TextUtils.isEmpty(key)) {
            throw new NullPointerException("key不能为空");
        }
        SharedPreferences sharedPreferences = getSharedPreferences(context, isCommon);
        if (sharedPreferences != null) {
            sharedPreferences.edit().putString(key, value).commit();
        }
    }

    /**
     * 保存布尔型值.
     *
     * @param context  Context
     * @param key      键
     * @param value    值
     * @param isCommon 是否属于公共属性, true - 属于公共属性, false - 属于私有属性
     */
    public static void setBoolean(
            final Context context, final String key, final boolean value, final boolean isCommon) {
        if (TextUtils.isEmpty(key)) {
            throw new NullPointerException("key不能为空");
        }
        SharedPreferences sharedPreferences = getSharedPreferences(context, isCommon);
        if (sharedPreferences != null) {
            sharedPreferences.edit().putBoolean(key, value).commit();
        }
    }

    /**
     * 保存整型值.
     *
     * @param context  Context
     * @param key      键
     * @param value    值
     * @param isCommon 是否属于公共属性, true - 属于公共属性, false - 属于私有属性
     */
    public static void setInt(
            final Context context, final String key, final int value, final boolean isCommon) {
        if (TextUtils.isEmpty(key)) {
            throw new NullPointerException("key不能为空");
        }
        SharedPreferences sharedPreferences = getSharedPreferences(context, isCommon);
        if (sharedPreferences != null) {
            sharedPreferences.edit().putInt(key, value).commit();
        }
    }

    /**
     * 保存浮点型值.
     *
     * @param context  Context
     * @param key      键
     * @param value    值
     * @param isCommon 是否属于公共属性, true - 属于公共属性, false - 属于私有属性
     */
    public static void setFloat(
            final Context context, final String key, final float value, final boolean isCommon) {
        if (TextUtils.isEmpty(key)) {
            throw new NullPointerException("key不能为空");
        }
        SharedPreferences sharedPreferences = getSharedPreferences(context, isCommon);
        if (sharedPreferences != null) {
            sharedPreferences.edit().putFloat(key, value).commit();
        }
    }

    /**
     * 保存长整型值.
     *
     * @param context  Context
     * @param key      键
     * @param value    值
     * @param isCommon 是否属于公共属性, true - 属于公共属性, false - 属于私有属性
     */
    public static void setLong(
            final Context context, final String key, final long value, final boolean isCommon) {
        if (TextUtils.isEmpty(key)) {
            throw new NullPointerException("key不能为空");
        }
        SharedPreferences sharedPreferences = getSharedPreferences(context, isCommon);
        if (sharedPreferences != null) {
            sharedPreferences.edit().putLong(key, value).commit();
        }
    }

    /**
     * 获取字符串型值.
     *
     * @param context  Context
     * @param key      键
     * @param isCommon 是否属于公共属性, true - 属于公共属性, false - 属于私有属性
     */
    public static String getString(
            final Context context, final String key, final boolean isCommon) {
        if (TextUtils.isEmpty(key)) {
            throw new NullPointerException("key不能为空");
        }
        SharedPreferences sharedPreferences = getSharedPreferences(context, isCommon);
        if (sharedPreferences != null) {
            return sharedPreferences.getString(key, STRING_DEFAULT);
        }
        return STRING_DEFAULT;
    }

    /**
     * 获取字符串型值.
     *
     * @param context  Context
     * @param key      键
     * @param defValue 默认值
     * @param isCommon 是否属于公共属性, true - 属于公共属性, false - 属于私有属性
     */
    public static String getString(
            final Context context,
            final String key,
            final String defValue,
            final boolean isCommon) {
        SharedPreferences sharedPreferences = getSharedPreferences(context, isCommon);
        if (sharedPreferences != null) {
            return sharedPreferences.getString(key, defValue);
        }

        return defValue;
    }

    /**
     * 获取布尔型值.
     *
     * @param context  Context
     * @param key      键
     * @param isCommon 是否属于公共属性, true - 属于公共属性, false - 属于私有属性
     */
    public static Boolean getBoolean(
            final Context context, final String key, final boolean isCommon) {
        if (TextUtils.isEmpty(key)) {
            throw new NullPointerException("key不能为空");
        }
        SharedPreferences sharedPreferences = getSharedPreferences(context, isCommon);
        if (sharedPreferences != null) {
            return sharedPreferences.getBoolean(key, BOOLEAN_DEFAULT);
        }
        return BOOLEAN_DEFAULT;
    }

    /**
     * 获取布尔型值.
     *
     * @param context  Context
     * @param key      键
     * @param defValue 默认值
     * @param isCommon 是否属于公共属性, true - 属于公共属性, false - 属于私有属性
     */
    public static Boolean getBoolean(
            final Context context, final String key, final boolean defValue, boolean isCommon) {
        if (TextUtils.isEmpty(key)) {
            throw new NullPointerException("key不能为空");
        }
        SharedPreferences sharedPreferences = getSharedPreferences(context, isCommon);
        if (sharedPreferences != null) {
            return sharedPreferences.getBoolean(key, defValue);
        }
        return defValue;
    }

    /**
     * 获取整型型值.
     *
     * @param context  Context
     * @param key      键
     * @param isCommon 是否属于公共属性, true - 属于公共属性, false - 属于私有属性
     */
    public static int getInt(final Context context, final String key, final boolean isCommon) {
        if (TextUtils.isEmpty(key)) {
            throw new NullPointerException("key不能为空");
        }
        SharedPreferences sharedPreferences = getSharedPreferences(context, isCommon);
        if (sharedPreferences != null) {
            return sharedPreferences.getInt(key, INT_DEFAULT);
        }
        return INT_DEFAULT;
    }

    /**
     * 获取整型型值,默认返回1.
     *
     * @param context  Context
     * @param key      键
     * @param isCommon 是否属于公共属性, true - 属于公共属性, false - 属于私有属性
     */
    public static int getIntNew(final Context context, final String key, final boolean isCommon) {
        if (TextUtils.isEmpty(key)) {
            throw new NullPointerException("key不能为空");
        }
        SharedPreferences sharedPreferences = getSharedPreferences(context, isCommon);
        if (sharedPreferences != null) {
            return sharedPreferences.getInt(key, 1);
        }
        return 1;
    }

    /**
     * 获取浮点型型值.
     *
     * @param context  Context
     * @param key      键
     * @param isCommon 是否属于公共属性, true - 属于公共属性, false - 属于私有属性
     */
    public static float getFloat(final Context context, final String key, final boolean isCommon) {
        if (TextUtils.isEmpty(key)) {
            throw new NullPointerException("key不能为空");
        }
        SharedPreferences sharedPreferences = getSharedPreferences(context, isCommon);
        if (sharedPreferences != null) {
            return sharedPreferences.getFloat(key, FLOAT_DEFAULT);
        }
        return FLOAT_DEFAULT;
    }

    /**
     * 获取字符串型值.
     *
     * @param context  Context
     * @param key      键
     * @param isCommon 是否属于公共属性, true - 属于公共属性, false - 属于私有属性
     */
    public static long getLong(final Context context, final String key, final boolean isCommon) {
        if (TextUtils.isEmpty(key)) {
            throw new NullPointerException("key不能为空");
        }
        SharedPreferences sharedPreferences = getSharedPreferences(context, isCommon);
        if (sharedPreferences != null) {
            return sharedPreferences.getLong(key, LONG_DEFAULT);
        }
        return LONG_DEFAULT;
    }

    /**
     * 根据获取合适的SharedPreferences.
     *
     * @param context  Context
     * @param isCommon 是否属于公共属性, true - 属于公共属性, false - 属于私有属性
     * @return 合适的SharedPreferences
     */
    private static SharedPreferences getSharedPreferences(Context context, boolean isCommon) {
        if (context == null) {
            throw new NullPointerException("context不能为空");
        }
        SharedPreferences sharedPreferences;
        if (isCommon) {
            sharedPreferences = getCommonPreferences(context);
        } else {
            sharedPreferences = getAccountPreferences(context);
        }
        return sharedPreferences;
    }

    /**
     * 获取公共的Preferences.
     *
     * @param context Context
     * @return 公共的Preferences
     */
    private static SharedPreferences getCommonPreferences(final Context context) {
        return context.getSharedPreferences(Preferences.COMMON, Context.MODE_PRIVATE);
    }

    /**
     * 获取登录帐号的Preferences.单独存储到一个文件中
     *
     * @param context Context
     * @return 登录帐号的Preferences, 若没有登录返回null
     */
    private static SharedPreferences getAccountPreferences(final Context context) {
        String account = getAccount(context);
        if (!STRING_DEFAULT.equals(account)) {
            return context.getSharedPreferences(account, Context.MODE_PRIVATE);
        }
        return null;
    }

    /**
     * 保存商品的浏览记录
     */
    public static void saveSearchGoodsHistory(final Context context, String history) {
        SharedPreferences sharedPreferences = getCommonPreferences(context);
        String oldText = sharedPreferences.getString(Preferences.SEARCH_GOODS_HISTORY, "");
        if (!TextUtils.isEmpty(history) && !oldText.contains(history)) {
            Editor editor = sharedPreferences.edit();
            editor.putString(Preferences.SEARCH_GOODS_HISTORY, history + "," + oldText);
            editor.commit();
        }
    }

    /**
     * 获取商品的浏览记录
     */
    public static List<Tag> getSearchGoodsHistory(final Context context) {
        SharedPreferences sharedPreferences = getCommonPreferences(context);
        String history = sharedPreferences.getString(Preferences.SEARCH_GOODS_HISTORY, "");
        List<Tag> list = new ArrayList<>();
        if (!TextUtils.isEmpty(history)) {
            for (String string : history.split(",")) {
                list.add(new Tag(string));
            }
        }
        return list;
    }

    /**
     * 清空商品的浏览记录
     */
    public static void clearSearchGoodsHistory(final Context context) {
        SharedPreferences sharedPreferences = getCommonPreferences(context);
        Editor editor = sharedPreferences.edit();
        editor.remove(Preferences.SEARCH_GOODS_HISTORY);
        editor.commit();
    }

    /**
     * 保存商店的浏览记录
     */
    public static void SaveSearchShopsHistory(final Context context, String history) {
        SharedPreferences sharedPreferences = getCommonPreferences(context);
        String oldText = sharedPreferences.getString(Preferences.SEARCH_SHOPS_HISTORY, "");
        if (!TextUtils.isEmpty(history) && !oldText.contains(history)) {
            Editor editor = sharedPreferences.edit();
            editor.putString(Preferences.SEARCH_SHOPS_HISTORY, history + "," + oldText);
            editor.commit();
        }
    }

    /**
     * 获取商店的浏览记录
     */
    public static List<Tag> getSearchShopsHistory(final Context context) {
        SharedPreferences sharedPreferences = getCommonPreferences(context);
        String history = sharedPreferences.getString(Preferences.SEARCH_SHOPS_HISTORY, "");
        List<Tag> list = new ArrayList<>();
        if (!TextUtils.isEmpty(history)) {
            for (String string : history.split(",")) {
                list.add(new Tag(string));
            }
        }
        return list;
    }

    /**
     * 清空商店的浏览记录
     */
    public static void clearSearchShopsHistory(final Context context) {
        SharedPreferences sharedPreferences = getCommonPreferences(context);
        Editor editor = sharedPreferences.edit();
        editor.remove(Preferences.SEARCH_SHOPS_HISTORY);
        editor.commit();
    }

    /**
     * 保存品牌的浏览记录
     */
    public static void saveSearchBrandHistory(final Context context, String history) {
        SharedPreferences sharedPreferences = getCommonPreferences(context);
        String oldText = sharedPreferences.getString(Preferences.SEARCH_BRAND_HISTORY, "");
        if (!TextUtils.isEmpty(history) && !oldText.contains(history)) {
            Editor editor = sharedPreferences.edit();
            editor.putString(Preferences.SEARCH_BRAND_HISTORY, history + "," + oldText);
            editor.commit();
        }
    }

    /**
     * 清空品牌的浏览记录
     */
    public static List<Tag> getSearchBrandHistory(final Context context) {
        SharedPreferences sharedPreferences = getCommonPreferences(context);
        String history = sharedPreferences.getString(Preferences.SEARCH_BRAND_HISTORY, "");
        List<Tag> list = new ArrayList<>();
        if (!TextUtils.isEmpty(history)) {
            for (String string : history.split(",")) {
                list.add(new Tag(string));
            }
        }
        return list;
    }

    /**
     * 清空品牌的浏览记录
     */
    public static void clearSearchBrandHistory(final Context context) {
        SharedPreferences sharedPreferences = getCommonPreferences(context);
        Editor editor = sharedPreferences.edit();
        editor.remove(Preferences.SEARCH_BRAND_HISTORY);
        editor.commit();
    }

    /**
     * 保存品牌的浏览记录
     */
    public static void saveDiscoverySearchHistory(final Context context, String history) {
        SharedPreferences sharedPreferences = getCommonPreferences(context);
        String oldText = sharedPreferences.getString(Preferences.DISCOVERY_SEARCH, "");
        if (!TextUtils.isEmpty(history) && !oldText.contains(history)) {
            Editor editor = sharedPreferences.edit();
            editor.putString(Preferences.DISCOVERY_SEARCH, history + "," + oldText);
            editor.commit();
        }
    }

    /**
     * 获取发现搜索记录
     */
    public static List<String> getDiscoverySearchHistory(final Context context) {
        SharedPreferences sharedPreferences = getCommonPreferences(context);
        String history = sharedPreferences.getString(Preferences.DISCOVERY_SEARCH, "");
        List<String> list = new ArrayList<>();
        if (!TextUtils.isEmpty(history)) {
            for (String content : history.split(",")) {
                list.add(content);
            }
        }
        return list;
    }

    /**
     * 清空发现搜索记录
     */
    public static void clearDiscoverySearchHistory(final Context context) {
        SharedPreferences sharedPreferences = getCommonPreferences(context);
        Editor editor = sharedPreferences.edit();
        editor.remove(Preferences.DISCOVERY_SEARCH);
        editor.commit();
    }

    /**
     * 保存本日签到记录
     *
     * @param userId 签到用户的id
     */
    /*
    public static void SaveSignUserId(final Context context, String userId) {
        SharedPreferences sharedPreferences = getCommonPreferences(context);
        String oldText = sharedPreferences.getString(Preferences.SIGN_HISTORY, "");
        if (!TextUtils.isEmpty(userId) && !oldText.contains(userId)) {
            Editor editor = sharedPreferences.edit();
            editor.putString(Preferences.SIGN_HISTORY, userId + "," + oldText);
            editor.commit();
        }
    }

    */
    /** 获取本日签到记录 */
    /*
    public static List<String> getSignUserId(final Context context) {
        SharedPreferences sharedPreferences = getCommonPreferences(context);
        String history = sharedPreferences.getString(Preferences.SIGN_HISTORY, "");
        List<String> list = new ArrayList<>();
        if (!TextUtils.isEmpty(history)) {
            for (String string : history.split(",")) {
                list.add(string);
            }
        }
        return list;
    }

    */
    /** 清空本日签到记录 */
    /*
    public static void clearSignUserId(final Context context) {
        SharedPreferences sharedPreferences = getCommonPreferences(context);
        Editor editor = sharedPreferences.edit();
        editor.remove(Preferences.SIGN_HISTORY);
        editor.commit();
    }*/

}
